﻿using System.Collections.Generic;
using System.Linq;
using WebSelfHost.Logic.Sys;
using WebSelfHost.Utility.ResponseModels;
using WebSelfHost.Utility.Core;
using WebSelfHost.Web.Filters;
using WebSelfHost.Utility.Operator;

using Microsoft.AspNetCore.Mvc;
using WebSelfHost.Entity.WebRequestParms.Sys.RoleAuthorize;
using WebSelfHost.Entity.WebRequestParms; 

#if NETFRAMEWORK
using System.Web.Http;
using OwinSessionMiddleware.WebApi;
#else
using Microsoft.AspNetCore.Http;
#endif
namespace WebSelfHost.Web.Controllers.Sys
{
    [HiddenApi]
    public class RoleAuthorizeController : BaseController
    {
        private SysRoleAuthorizeLogic roleAuthorizeLogic;
        private SysPermissionLogic permissionLogic;

        public RoleAuthorizeController()
        {
            roleAuthorizeLogic = new SysRoleAuthorizeLogic();
            permissionLogic = new SysPermissionLogic();
        }


        [HttpGet, Route("system/roleAuthorize/index"), AuthorizeChecked]
        public ActionResult Index()
        {
            return Html("/Sys/RoleAuthorize/Index");
        }

        /// <summary>
        /// 角色授权权限数据
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/roleAuthorize/index"), AuthorizeChecked]
        public ActionResult Index(PrimaryKeyParms parms)
        {
            var listPerIds = roleAuthorizeLogic.GetList(parms.roleId).Select(c => c.ModuleId).ToList();
            var listAllPers = permissionLogic.GetList();
            List<ZTreeNode> result = new List<ZTreeNode>();
            foreach (var item in listAllPers)
            {
                ZTreeNode model = new ZTreeNode();
                model.@checked = listPerIds.Contains(item.Id) ? model.@checked = true : model.@checked = false;
                model.id = item.Id;
                model.pId = item.ParentId;
                model.name = item.Name;
                model.open = true;
                result.Add(model);
            }
            return Content(result.ToJson());
        }

        /// <summary>
        /// 角色授权权限数据提交
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/roleAuthorize/form"), LoginChecked]
        public ActionResult Form(AuthorParms parms)
        {
            roleAuthorizeLogic.Authorize(parms.roleId, OperatorProvider.Instance.GetCurrent(Request).Account, parms.perIds.SplitToList().ToArray());
            return Success("授权成功");
        }


        /// <summary>
        /// 角色授权权限数据
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/roleAuthorize/index")]
        public ActionResult AppRoleAuthorizeData(PrimaryKeyParms parms)
        {
            var listPerIds = roleAuthorizeLogic.GetList(parms.roleId).Select(c => c.ModuleId).ToList();
            var listAllPers = permissionLogic.GetList();
            List<ZTreeNode> result = new List<ZTreeNode>();
            foreach (var item in listAllPers)
            {
                ZTreeNode model = new ZTreeNode();
                model.@checked = listPerIds.Contains(item.Id) ? model.@checked = true : model.@checked = false;
                model.id = item.Id;
                model.pId = item.ParentId;
                model.name = item.Name;
                model.open = true;
                result.Add(model);
            }
            return Content(result.ToJson());
        }

        /// <summary>
        /// 角色授权权限数据提交
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/roleAuthorize/form")]
        public ActionResult AppForm(AuthorParms parms)
        {
            roleAuthorizeLogic.AppAuthorize(parms.operater, parms.roleId, parms.perIds.SplitToList().ToArray());
            return Success("授权成功");
        }
    }
}
